﻿using CRM.Database.Model;

namespace CRM.Syncroniser
{
  public  class TrainersSyncroniser:ISyncronise
    {

      /// <summary>
      /// Тренеры из CDB
      /// </summary>
      public DbInterface.LearningTrainers CDBTrainers
      {
          get { return DbInterface.DataLayer.LearningTrainers; }
      }



      /// <summary>
      /// Тренеры из CRM
      /// </summary>
      public CRM.Database.Model.TrainingsTrainersCollection CRMTrainers
      {
          get { return new TrainingsTrainersCollection(); }
      }


      #region Implementation of ISyncronise

      /// <summary>
      /// Запуск синхронизации
      /// </summary>
      /// <returns>Результат запуска</returns>
      public bool Syncronise()
      {
          foreach(var item in CDBTrainers)
          {
              var tr = new TrainingsTrainers();
              tr.Name = item.Name;
              tr.Phone = item.Phone;
              tr.Email = item.Email;
              tr.IsActive = true;

              var colTC = new TrainingsCentersCollection();
              int tc = colTC.GetIdByName(item.LearningProvider.Name);

              if (tc > 0)
                  tr.IdTrainingsCenter = tc;

              if (CRMTrainers.GetItemByName(tr.Name) == null)
                  CRMTrainers.Add(tr,"Max");
          }

          return true;
      }

      #endregion
    }
}
